package com.security.hikapp.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

public class SharedPreUtil {
    public static final String PREF_CONFIG =  "security_app_config";
    private static SharedPreferences pref;
    private static SharedPreferences.Editor editor;

    public static void init(Context context) {
        pref = context.getSharedPreferences(PREF_CONFIG, Context.MODE_PRIVATE);
        editor = pref.edit();
    }

    public static float getFloat(String key, float defValue) {
        return pref != null ? pref.getFloat(key, defValue) : defValue;
    }

    public static int getInt(String key, int defValue) {
        return pref != null ? pref.getInt(key, defValue) : defValue;
    }

    public static long getLong(String key, long defValue) {
        return pref != null ? pref.getLong(key, defValue) : defValue;
    }

    public static String getString(String key) {
        return pref != null ? pref.getString(key, "") : "";
    }

    public static String getString(String key, String defValue) {
        return pref != null ? pref.getString(key, defValue) : defValue;
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return pref != null ? pref.getBoolean(key, defValue) : defValue;
    }

    public static Map<String, ?> getAll() {
        return pref != null ? pref.getAll() : null;
    }

    public static void putFloat(String key, float value) {
        if (editor == null) return;
        editor.putFloat(key, value);
        editor.commit();
    }

    public static void putInt(String key, int value) {
        if (editor == null) return;
        editor.putInt(key, value);
        editor.commit();
    }

    public static void putLong(String key, long value) {
        if (editor == null) return;
        editor.putLong(key, value);
        editor.commit();
    }

    public static void putString(String key, String value) {
        if (editor == null) return;
        editor.putString(key, value);
        editor.commit();
    }

    public static void putBoolean(String key, boolean value) {
        if (editor == null) return;
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void remove(String key) {
        if (editor == null) return;
        editor.remove(key);
    }
    /**
     * 保存
     */
    public static void setSp(Context context,String name, String key, String value){
        SharedPreferences sp = context.getSharedPreferences(name,0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key,value);
        editor.commit();
    }

    /**
     * 获取
     */
    public static String getSp(Context context, String name, String key, String defaultValue){
        SharedPreferences sp = context.getSharedPreferences(name,0);
        String value = sp.getString(key,defaultValue);
        return value;
    }
}
